Option Strict On
Option Explicit On 

Public Class Expression
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnApply As System.Windows.Forms.Button
    Friend WithEvents btnOk As System.Windows.Forms.Button
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents Expression1 As Avand.ExpressionControl
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Expression))
        Me.btnApply = New System.Windows.Forms.Button
        Me.btnOk = New System.Windows.Forms.Button
        Me.btnCancel = New System.Windows.Forms.Button
        Me.TabControl1 = New System.Windows.Forms.TabControl
        Me.TabPage1 = New System.Windows.Forms.TabPage
        Me.Expression1 = New Avand.ExpressionControl
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.SuspendLayout()
        '
        'btnApply
        '
        Me.btnApply.AccessibleDescription = resources.GetString("btnApply.AccessibleDescription")
        Me.btnApply.AccessibleName = resources.GetString("btnApply.AccessibleName")
        Me.btnApply.Anchor = CType(resources.GetObject("btnApply.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.btnApply.BackgroundImage = CType(resources.GetObject("btnApply.BackgroundImage"), System.Drawing.Image)
        Me.btnApply.Dock = CType(resources.GetObject("btnApply.Dock"), System.Windows.Forms.DockStyle)
        Me.btnApply.Enabled = CType(resources.GetObject("btnApply.Enabled"), Boolean)
        Me.btnApply.FlatStyle = CType(resources.GetObject("btnApply.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.btnApply.Font = CType(resources.GetObject("btnApply.Font"), System.Drawing.Font)
        Me.btnApply.Image = CType(resources.GetObject("btnApply.Image"), System.Drawing.Image)
        Me.btnApply.ImageAlign = CType(resources.GetObject("btnApply.ImageAlign"), System.Drawing.ContentAlignment)
        Me.btnApply.ImageIndex = CType(resources.GetObject("btnApply.ImageIndex"), Integer)
        Me.btnApply.ImeMode = CType(resources.GetObject("btnApply.ImeMode"), System.Windows.Forms.ImeMode)
        Me.btnApply.Location = CType(resources.GetObject("btnApply.Location"), System.Drawing.Point)
        Me.btnApply.Name = "btnApply"
        Me.btnApply.RightToLeft = CType(resources.GetObject("btnApply.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.btnApply.Size = CType(resources.GetObject("btnApply.Size"), System.Drawing.Size)
        Me.btnApply.TabIndex = CType(resources.GetObject("btnApply.TabIndex"), Integer)
        Me.btnApply.Text = resources.GetString("btnApply.Text")
        Me.btnApply.TextAlign = CType(resources.GetObject("btnApply.TextAlign"), System.Drawing.ContentAlignment)
        Me.btnApply.Visible = CType(resources.GetObject("btnApply.Visible"), Boolean)
        '
        'btnOk
        '
        Me.btnOk.AccessibleDescription = resources.GetString("btnOk.AccessibleDescription")
        Me.btnOk.AccessibleName = resources.GetString("btnOk.AccessibleName")
        Me.btnOk.Anchor = CType(resources.GetObject("btnOk.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.btnOk.BackgroundImage = CType(resources.GetObject("btnOk.BackgroundImage"), System.Drawing.Image)
        Me.btnOk.Dock = CType(resources.GetObject("btnOk.Dock"), System.Windows.Forms.DockStyle)
        Me.btnOk.Enabled = CType(resources.GetObject("btnOk.Enabled"), Boolean)
        Me.btnOk.FlatStyle = CType(resources.GetObject("btnOk.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.btnOk.Font = CType(resources.GetObject("btnOk.Font"), System.Drawing.Font)
        Me.btnOk.Image = CType(resources.GetObject("btnOk.Image"), System.Drawing.Image)
        Me.btnOk.ImageAlign = CType(resources.GetObject("btnOk.ImageAlign"), System.Drawing.ContentAlignment)
        Me.btnOk.ImageIndex = CType(resources.GetObject("btnOk.ImageIndex"), Integer)
        Me.btnOk.ImeMode = CType(resources.GetObject("btnOk.ImeMode"), System.Windows.Forms.ImeMode)
        Me.btnOk.Location = CType(resources.GetObject("btnOk.Location"), System.Drawing.Point)
        Me.btnOk.Name = "btnOk"
        Me.btnOk.RightToLeft = CType(resources.GetObject("btnOk.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.btnOk.Size = CType(resources.GetObject("btnOk.Size"), System.Drawing.Size)
        Me.btnOk.TabIndex = CType(resources.GetObject("btnOk.TabIndex"), Integer)
        Me.btnOk.Text = resources.GetString("btnOk.Text")
        Me.btnOk.TextAlign = CType(resources.GetObject("btnOk.TextAlign"), System.Drawing.ContentAlignment)
        Me.btnOk.Visible = CType(resources.GetObject("btnOk.Visible"), Boolean)
        '
        'btnCancel
        '
        Me.btnCancel.AccessibleDescription = resources.GetString("btnCancel.AccessibleDescription")
        Me.btnCancel.AccessibleName = resources.GetString("btnCancel.AccessibleName")
        Me.btnCancel.Anchor = CType(resources.GetObject("btnCancel.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.btnCancel.BackgroundImage = CType(resources.GetObject("btnCancel.BackgroundImage"), System.Drawing.Image)
        Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnCancel.Dock = CType(resources.GetObject("btnCancel.Dock"), System.Windows.Forms.DockStyle)
        Me.btnCancel.Enabled = CType(resources.GetObject("btnCancel.Enabled"), Boolean)
        Me.btnCancel.FlatStyle = CType(resources.GetObject("btnCancel.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.btnCancel.Font = CType(resources.GetObject("btnCancel.Font"), System.Drawing.Font)
        Me.btnCancel.Image = CType(resources.GetObject("btnCancel.Image"), System.Drawing.Image)
        Me.btnCancel.ImageAlign = CType(resources.GetObject("btnCancel.ImageAlign"), System.Drawing.ContentAlignment)
        Me.btnCancel.ImageIndex = CType(resources.GetObject("btnCancel.ImageIndex"), Integer)
        Me.btnCancel.ImeMode = CType(resources.GetObject("btnCancel.ImeMode"), System.Windows.Forms.ImeMode)
        Me.btnCancel.Location = CType(resources.GetObject("btnCancel.Location"), System.Drawing.Point)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.RightToLeft = CType(resources.GetObject("btnCancel.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.btnCancel.Size = CType(resources.GetObject("btnCancel.Size"), System.Drawing.Size)
        Me.btnCancel.TabIndex = CType(resources.GetObject("btnCancel.TabIndex"), Integer)
        Me.btnCancel.Text = resources.GetString("btnCancel.Text")
        Me.btnCancel.TextAlign = CType(resources.GetObject("btnCancel.TextAlign"), System.Drawing.ContentAlignment)
        Me.btnCancel.Visible = CType(resources.GetObject("btnCancel.Visible"), Boolean)
        '
        'TabControl1
        '
        Me.TabControl1.AccessibleDescription = resources.GetString("TabControl1.AccessibleDescription")
        Me.TabControl1.AccessibleName = resources.GetString("TabControl1.AccessibleName")
        Me.TabControl1.Alignment = CType(resources.GetObject("TabControl1.Alignment"), System.Windows.Forms.TabAlignment)
        Me.TabControl1.Anchor = CType(resources.GetObject("TabControl1.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.TabControl1.Appearance = CType(resources.GetObject("TabControl1.Appearance"), System.Windows.Forms.TabAppearance)
        Me.TabControl1.BackgroundImage = CType(resources.GetObject("TabControl1.BackgroundImage"), System.Drawing.Image)
        Me.TabControl1.Controls.Add(Me.TabPage1)
        Me.TabControl1.Dock = CType(resources.GetObject("TabControl1.Dock"), System.Windows.Forms.DockStyle)
        Me.TabControl1.Enabled = CType(resources.GetObject("TabControl1.Enabled"), Boolean)
        Me.TabControl1.Font = CType(resources.GetObject("TabControl1.Font"), System.Drawing.Font)
        Me.TabControl1.ImeMode = CType(resources.GetObject("TabControl1.ImeMode"), System.Windows.Forms.ImeMode)
        Me.TabControl1.ItemSize = CType(resources.GetObject("TabControl1.ItemSize"), System.Drawing.Size)
        Me.TabControl1.Location = CType(resources.GetObject("TabControl1.Location"), System.Drawing.Point)
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.Padding = CType(resources.GetObject("TabControl1.Padding"), System.Drawing.Point)
        Me.TabControl1.RightToLeft = CType(resources.GetObject("TabControl1.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.ShowToolTips = CType(resources.GetObject("TabControl1.ShowToolTips"), Boolean)
        Me.TabControl1.Size = CType(resources.GetObject("TabControl1.Size"), System.Drawing.Size)
        Me.TabControl1.TabIndex = CType(resources.GetObject("TabControl1.TabIndex"), Integer)
        Me.TabControl1.Text = resources.GetString("TabControl1.Text")
        Me.TabControl1.Visible = CType(resources.GetObject("TabControl1.Visible"), Boolean)
        '
        'TabPage1
        '
        Me.TabPage1.AccessibleDescription = resources.GetString("TabPage1.AccessibleDescription")
        Me.TabPage1.AccessibleName = resources.GetString("TabPage1.AccessibleName")
        Me.TabPage1.Anchor = CType(resources.GetObject("TabPage1.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.TabPage1.AutoScroll = CType(resources.GetObject("TabPage1.AutoScroll"), Boolean)
        Me.TabPage1.AutoScrollMargin = CType(resources.GetObject("TabPage1.AutoScrollMargin"), System.Drawing.Size)
        Me.TabPage1.AutoScrollMinSize = CType(resources.GetObject("TabPage1.AutoScrollMinSize"), System.Drawing.Size)
        Me.TabPage1.BackgroundImage = CType(resources.GetObject("TabPage1.BackgroundImage"), System.Drawing.Image)
        Me.TabPage1.Controls.Add(Me.Expression1)
        Me.TabPage1.Dock = CType(resources.GetObject("TabPage1.Dock"), System.Windows.Forms.DockStyle)
        Me.TabPage1.Enabled = CType(resources.GetObject("TabPage1.Enabled"), Boolean)
        Me.TabPage1.Font = CType(resources.GetObject("TabPage1.Font"), System.Drawing.Font)
        Me.TabPage1.ImageIndex = CType(resources.GetObject("TabPage1.ImageIndex"), Integer)
        Me.TabPage1.ImeMode = CType(resources.GetObject("TabPage1.ImeMode"), System.Windows.Forms.ImeMode)
        Me.TabPage1.Location = CType(resources.GetObject("TabPage1.Location"), System.Drawing.Point)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.RightToLeft = CType(resources.GetObject("TabPage1.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.TabPage1.Size = CType(resources.GetObject("TabPage1.Size"), System.Drawing.Size)
        Me.TabPage1.TabIndex = CType(resources.GetObject("TabPage1.TabIndex"), Integer)
        Me.TabPage1.Text = resources.GetString("TabPage1.Text")
        Me.TabPage1.ToolTipText = resources.GetString("TabPage1.ToolTipText")
        Me.TabPage1.Visible = CType(resources.GetObject("TabPage1.Visible"), Boolean)
        '
        'Expression1
        '
        Me.Expression1.AccessibleDescription = resources.GetString("Expression1.AccessibleDescription")
        Me.Expression1.AccessibleName = resources.GetString("Expression1.AccessibleName")
        Me.Expression1.Anchor = CType(resources.GetObject("Expression1.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.Expression1.AutoScroll = CType(resources.GetObject("Expression1.AutoScroll"), Boolean)
        Me.Expression1.AutoScrollMargin = CType(resources.GetObject("Expression1.AutoScrollMargin"), System.Drawing.Size)
        Me.Expression1.AutoScrollMinSize = CType(resources.GetObject("Expression1.AutoScrollMinSize"), System.Drawing.Size)
        Me.Expression1.BackgroundImage = CType(resources.GetObject("Expression1.BackgroundImage"), System.Drawing.Image)
        Me.Expression1.Dock = CType(resources.GetObject("Expression1.Dock"), System.Windows.Forms.DockStyle)
        Me.Expression1.Enabled = CType(resources.GetObject("Expression1.Enabled"), Boolean)
        Me.Expression1.Expression = ""
        Me.Expression1.ExpressionType = Model10.ExpressionType.Filter
        Me.Expression1.Font = CType(resources.GetObject("Expression1.Font"), System.Drawing.Font)
        Me.Expression1.ImeMode = CType(resources.GetObject("Expression1.ImeMode"), System.Windows.Forms.ImeMode)
        Me.Expression1.Location = CType(resources.GetObject("Expression1.Location"), System.Drawing.Point)
        Me.Expression1.Name = "Expression1"
        Me.Expression1.RightToLeft = CType(resources.GetObject("Expression1.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.Expression1.Size = CType(resources.GetObject("Expression1.Size"), System.Drawing.Size)
        Me.Expression1.TabIndex = CType(resources.GetObject("Expression1.TabIndex"), Integer)
        Me.Expression1.Table = Nothing
        Me.Expression1.Visible = CType(resources.GetObject("Expression1.Visible"), Boolean)
        '
        'Expression
        '
        Me.AcceptButton = Me.btnOk
        Me.AccessibleDescription = resources.GetString("$this.AccessibleDescription")
        Me.AccessibleName = resources.GetString("$this.AccessibleName")
        Me.AutoScaleBaseSize = CType(resources.GetObject("$this.AutoScaleBaseSize"), System.Drawing.Size)
        Me.AutoScroll = CType(resources.GetObject("$this.AutoScroll"), Boolean)
        Me.AutoScrollMargin = CType(resources.GetObject("$this.AutoScrollMargin"), System.Drawing.Size)
        Me.AutoScrollMinSize = CType(resources.GetObject("$this.AutoScrollMinSize"), System.Drawing.Size)
        Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
        Me.CancelButton = Me.btnCancel
        Me.ClientSize = CType(resources.GetObject("$this.ClientSize"), System.Drawing.Size)
        Me.Controls.Add(Me.TabControl1)
        Me.Controls.Add(Me.btnApply)
        Me.Controls.Add(Me.btnOk)
        Me.Controls.Add(Me.btnCancel)
        Me.Enabled = CType(resources.GetObject("$this.Enabled"), Boolean)
        Me.Font = CType(resources.GetObject("$this.Font"), System.Drawing.Font)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.ImeMode = CType(resources.GetObject("$this.ImeMode"), System.Windows.Forms.ImeMode)
        Me.Location = CType(resources.GetObject("$this.Location"), System.Drawing.Point)
        Me.MaximizeBox = False
        Me.MaximumSize = CType(resources.GetObject("$this.MaximumSize"), System.Drawing.Size)
        Me.MinimizeBox = False
        Me.MinimumSize = CType(resources.GetObject("$this.MinimumSize"), System.Drawing.Size)
        Me.Name = "Expression"
        Me.RightToLeft = CType(resources.GetObject("$this.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.ShowInTaskbar = False
        Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show
        Me.StartPosition = CType(resources.GetObject("$this.StartPosition"), System.Windows.Forms.FormStartPosition)
        Me.Text = resources.GetString("$this.Text")
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private mChanged As Boolean
    Private mExpressionType As Model10.ExpressionType
    Private mTable As Model10.Table

    Public Event ValueChenged As EventHandler

    Private Sub Advanced_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mChanged = False
        btnApply.Enabled = False

        Dim tp As TabPage
        For Each tp In TabControl1.TabPages
            Dim exp As Avand.ExpressionControl = CType(tp.Controls(0), Avand.ExpressionControl)
            Select Case mExpressionType
                Case ExpressionType.Filter : exp.Expression = mTable.Presentation.DataViewManager.DataViewSettings(exp.Table.DataTable).RowFilter
                Case ExpressionType.Sort : exp.Expression = mTable.Presentation.DataViewManager.DataViewSettings(exp.Table.DataTable).Sort
            End Select
            exp = Nothing
        Next : tp = Nothing
    End Sub
    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Application.DoEvents()
        Cursor.Current = Cursors.WaitCursor
        '----------------------------------
        Try
            Dim tp As TabPage
            For Each tp In TabControl1.TabPages
                Dim exp As Avand.ExpressionControl = CType(tp.Controls(0), Avand.ExpressionControl)
                Select Case mExpressionType
                    Case ExpressionType.Filter : mTable.Presentation.DataViewManager.DataViewSettings(exp.Table.DataTable).RowFilter = exp.Expression
                    Case ExpressionType.Sort : mTable.Presentation.DataViewManager.DataViewSettings(exp.Table.DataTable).Sort = exp.Expression
                End Select
                exp = Nothing
            Next : tp = Nothing

            mChanged = True
            btnApply.Enabled = False
            RaiseEvent ValueChenged(Me, New EventArgs)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        '----------------------------------
        Cursor.Current = Cursors.Default
    End Sub
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        btnApply.PerformClick()
        If Not btnApply.Enabled Then Me.DialogResult = DialogResult.OK
    End Sub

    Private Sub FillTable(ByVal table As Model10.Table)
        Dim tp As New TabPage(table.Name)
        TabControl1.TabPages.Add(tp)

        Dim exp As New Avand.ExpressionControl(mExpressionType, table)
        exp.Dock = DockStyle.Fill
        AddHandler exp.ValueChenged, AddressOf OnValueChanged
        tp.Controls.Add(exp)
        If table Is mTable Then TabControl1.SelectedTab = tp

        exp = Nothing
        tp = Nothing
    End Sub
    Protected Sub OnValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        btnApply.Enabled = True
        RaiseEvent ValueChenged(Me, New EventArgs)
    End Sub

    Public Property Changed() As Boolean
        Get
            Return mChanged
        End Get
        Set(ByVal Value As Boolean)
            mChanged = Value
        End Set
    End Property
    Public Property ExpressionType() As Model10.ExpressionType
        Get
            Return mExpressionType
        End Get
        Set(ByVal Value As Model10.ExpressionType)
            mExpressionType = Value
        End Set
    End Property
    Public Property Table() As Model10.Table
        Get
            Return mTable
        End Get
        Set(ByVal Value As Model10.Table)
            mTable = Value
        End Set
    End Property

    Public Sub Fill()
        Select Case mExpressionType
            Case ExpressionType.Filter : Me.Text = GetSystemResource("Expression.Filter")
            Case ExpressionType.Sort : Me.Text = GetSystemResource("Expression.Sort")
        End Select
        TabControl1.TabPages.Clear()

        Dim tbl As Model10.Table
        For Each tbl In mTable.Presentation.Tables
            FillTable(tbl)
        Next : tbl = Nothing
    End Sub
    Public Sub New(ByVal expressionType As Model10.ExpressionType, ByVal table As Model10.Table)
        MyBase.New()
        InitializeComponent()

        mExpressionType = expressionType
        mTable = table
        Fill()
    End Sub
End Class
